package com.iwinding.ladygg.ui.view;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.utils.CommonUtil;
import com.iwinding.ladygg.utils.RoundedTransformation;
import com.squareup.picasso.Picasso;

/**
 * Created by hezhiyong on 2017/4/17.
 */

public class SosDialogView extends LinearLayout implements View.OnClickListener {

    TextView send;
    TextView location;
    ImageView icon;
    Button left;
    Button right;

    public OnClick onClick;
//    public AlertDialog dialog;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public SosDialogView(Context context) {
        super(context);
        initView(context);
    }

    public SosDialogView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SosDialogView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    void initView(Context context) {
        mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        Display d = mWindowManager.getDefaultDisplay(); //为获取屏幕宽、高
        mWindowManagerParams = new WindowManager.LayoutParams();
        // 设置window type
        mWindowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        // 设置图片格式，效果为背景透明
        mWindowManagerParams.format = PixelFormat.RGBA_8888;
        // 设置Window flag
        mWindowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 调整悬浮窗口至左上角，便于调整坐标
        mWindowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
        // 以屏幕左上角为原点，设置x、y初始值
        mWindowManagerParams.x = 0;
        mWindowManagerParams.y = context.getResources().getDisplayMetrics().heightPixels / 3;
        // 设置悬浮窗口长宽数据
        mWindowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowManagerParams.height = WindowManager.LayoutParams.MATCH_PARENT;

        View.inflate(context, R.layout.icane_view_sos_warning, this);
        icon = (ImageView) findViewById(R.id.icane_user_icon);
        send = (TextView) findViewById(R.id.icane_sos_txt_send);
        location = (TextView) findViewById(R.id.icane_txt_location);
        left = (Button) findViewById(R.id.icane_know);
        right = (Button) findViewById(R.id.icane_view_details);
        send.setText(getContext().getString(R.string.icane_sos_initiate, "daye"));

        left.setOnClickListener(this);
        right.setOnClickListener(this);
//        mPopWindow = new PopupWindow(this, d.getWidth()*8/10,WindowManager.LayoutParams.WRAP_CONTENT, false);// 创建PopupWindow实例
    }

    public void setLocation(String locationStr) {
        location.setText(locationStr);
    }

    public void setSender(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        send.setText(getContext().getString(R.string.icane_sos_initiate, str));
    }

    public void setIconUrl(String iconUrl) {
        if (!TextUtils.isEmpty(iconUrl)) {
            String url = CommonUtil.utf8Togb2312(iconUrl);
            Picasso.with(getContext()).load(url).transform(new RoundedTransformation(9, 0)).into(icon);
        }
    }



    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:
//            case KeyEvent.KEYCODE_MENU:
                // 处理自己的逻辑break;
                hide();
            default:
                break;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icane_know:
                onClick.onClickLeft();
                hide();
                break;
            case R.id.icane_view_details:
                onClick.onClickRight(getContext());
                break;
        }
    }

    public interface OnClick {
        public void onClickLeft();

        public void onClickRight(Context context);
    }

//    public void openDialog(Context mContext) {
//        dismissDialog();
//        dialog = new AlertDialog.Builder(mContext)
//                .setView(this)
//                .setTitle(R.string.icane_sos_hint)
//                .create();
//        dialog.show();
//    }
//
//    public void dismissDialog(){
//        if (dialog != null) {
//            dialog.dismiss();
//        }
//    }

    private WindowManager				mWindowManager;
    private WindowManager.LayoutParams	mWindowManagerParams;
    private PopupWindow					mPopWindow;
    private boolean mIsShow;
    private int							mScreenWidth;

    public void show(){
        if (!mIsShow)
        {
            mScreenWidth = getResources().getDisplayMetrics().widthPixels;
            if (mWindowManagerParams.x < (mScreenWidth / 2))
            {
                mWindowManagerParams.x = mScreenWidth;
            }
            else
            {
                mWindowManagerParams.x = mScreenWidth;
            }
            mWindowManager.addView(this, mWindowManagerParams);
            mIsShow = true;
        }
    }

    public void hide(){
        if (mIsShow)
        {
//            if(mPopWindow.isShowing())
//            {
//                mPopWindow.dismiss();
//            }
            mWindowManager.removeView(this);
            mIsShow = false;
        }
    }
    @Override
    protected void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            if (mIsShow)
            {
//                if(mPopWindow.isShowing())
//                {
//                    mPopWindow.dismiss();
//                }
                mScreenWidth = getResources().getDisplayMetrics().widthPixels;
                if (mWindowManagerParams.x < (mScreenWidth / 2))
                {
                    mWindowManagerParams.x = mScreenWidth;
                }
                else
                {
                    mWindowManagerParams.x = mScreenWidth;
                }
                mWindowManager.updateViewLayout(this, mWindowManagerParams); // 刷新显示
            }
        }
    }

}
